/*
 *  Gurr - A multiprotocol chat client for Android
 *  Copyright (C) 2011 Malte Aschermann, Wilfried Ramaker
 *  
 *  This file is part of Gurr.
 *  
 *  Gurr is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Gurr is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Gurr.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 *  Gurr - A multiprotocol chat client for Android
 *  Copyright (C) 2011 Malte Aschermann, Wilfried Ramaker
 *  
 *  This file is part of Gurr.
 *  
 *  Gurr is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Gurr is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Gurr.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.masc.gurr.net.otr;

import java.io.IOException;

import ca.uwaterloo.crysp.otr.TLV;
import ca.uwaterloo.crysp.otr.iface.OTRInterface;
import ca.uwaterloo.crysp.otr.iface.OTRTLV;
import ca.uwaterloo.crysp.otr.iface.Policy;
import ca.uwaterloo.crysp.otr.iface.StringTLV;

public class Session {
	
	/** error return messages */
	public static final String DECRYPT_FAIL = "** DECRYPTION FAIL **";
	public static final String MSG_SEND_FAIL = "** MSG SEND FAIL **";		
	
	private String fStrProtocol;
	private LocalCallback fOTRCallback;
	
	private OTRState otrstate;
	private String fStrMyUin;
	private String fStrRecipientUin;
	private OTRInterface fUsICQ;
	
	public Session(String pStrMyUin, String pStrProtocol, String pStrRecipientUin, OTRInterface pUsICQ) throws IOException {
		fOTRCallback = new LocalCallback(this);
		fStrMyUin = pStrMyUin;
		fStrProtocol = pStrProtocol;
		fStrRecipientUin = pStrRecipientUin;
		fUsICQ = pUsICQ;
		otrstate = OTRState.NOT_SECURE;
	}
	
	public String getUin() {
		return fStrMyUin;
	}
	public void setUin(String pStrUin) {
		this.fStrMyUin = pStrUin;
	}
	
	public String send(String pStrMsg) {

		String strEncMsg=null;
		
		try {
			OTRTLV[] tlvs = new OTRTLV[1];
			tlvs[0]=new TLV(9, "TestTLV".getBytes());
			strEncMsg = fUsICQ.messageSending(fStrMyUin, fStrProtocol, fStrRecipientUin, pStrMsg, tlvs, Policy.FRAGMENT_SEND_ALL, fOTRCallback);
		} catch (Exception e) {
			return Session.MSG_SEND_FAIL;
		}
		
		return strEncMsg;
	}
	
	public String receive(String pStrMsg) {
		try {
			StringTLV stlv = fUsICQ.messageReceiving(fStrMyUin, fStrProtocol,
			        fStrRecipientUin, pStrMsg, fOTRCallback);
			
			if (stlv == null) { /** internal protocol message */
				return null;
			}
			else {
				return stlv.msg;
			}
		} catch (Exception e) {
			return Session.DECRYPT_FAIL;
		}
	}
	
	protected String getProtocolType() {
		return fStrProtocol;
	}
	protected void setProtocolType(String pStrProtocol) {
		this.fStrProtocol = pStrProtocol;
	}
	protected LocalCallback getOTRCallback() {
		return fOTRCallback;
	}
	protected void setOTRCallback(LocalCallback pOTRCallback) {
		this.fOTRCallback = pOTRCallback;
	}

	protected String getRecipientUin() {
		return fStrRecipientUin;
	}

	protected OTRState getOtrState() {
		return otrstate;
	}

	protected void setOtrState(OTRState otrstate) {
		this.otrstate = otrstate;
	}
}
